<template>
  <basicTopContainer v-loading="loading">
    <el-tabs v-model="activeName" slot="tabs" tab-position="bottom">
      <el-tab-pane name="first">
        <span slot="label" class="tabs-title text-center">支付订单</span>
      </el-tab-pane>
    </el-tabs>
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
      >
        <!-- <template slot="menuLeft">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            plain
            v-if="permission.recharge_delete"
            @click="handleDelete"
          >删 除</el-button>
        </template>-->
        <template slot-scope="scope" slot="createTimeSearch">
          <el-date-picker
            v-model="query.createTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </template>
        <template slot-scope="scope" slot="accountNameSearch">
          <el-input v-model="query.accountName" placeholder="选择结算户" @focus="dialogVisible = true"></el-input>
        </template>
        <template slot="searchMenu">
          <el-button type="primary" @click="getExcel">导出</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog title="选择客户" :visible.sync="dialogVisible" append-to-body>
      <avue-crud
        :option="optiondialog"
        :data="datadialog"
        :page="pagedialog"
        ref="dialogcrud"
        @search-change="searchChangeDialog"
        @on-load="onLoadCustor"
        @search-reset="searchResetDialog"
        @row-click="selectRow"
      ></avue-crud>
    </el-dialog>
  </basicTopContainer>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove,
  getCustorList,
} from "@/api/meter/recharge";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      dialogVisible: false,
      activeName: "first",
      form: {},
      query: {},
      customerName: "",
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      pagedialog: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optiondialog: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 10,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        addBtn: false,
        menu: false,
        dialogClickModal: false,
        searchLabelWidth: 50,
        column: [
          {
            label: "客户",
            prop: "customerName",
            search: true,
          },
          {
            label: "结算户",
            prop: "supplyCompany",
          },
        ],
      },
      option: {
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        index: true,
        viewBtn: true,
        selection: true,
        addBtn: false,
        menu: false,
        dialogClickModal: false,
        searchLabelWidth: 100,
        column: [
          {
            label: "充值流水号",
            prop: "sn",
            rules: [
              {
                required: true,
                message: "请输入充值编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "充值时间",
            prop: "createTime",
            searchslot: true,
            search: true,
            format: "yyyy-mm-dd HH:mm:ss",
            rules: [
              {
                required: true,
                message: "请输入充值编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "充值方式",
            prop: "chargeType",
            type: "select",
            search: true,
            dicData: [
              {
                label: "微信",
                value: "1",
              },
              {
                label: "支付宝",
                value: "2",
              },
              {
                label: "银联",
                value: "3",
              },
              {
                label: "线下充值",
                value: "4",
              },
            ],
            rules: [
              {
                required: true,
                message: "请输入充值方式: 1微信 2支付宝 3银联",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "充值用途: 1电费 2水费   3物业费",
          //   prop: "chargeTarget",
          //   hide: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入充值用途: 1电费 2水费   3物业费",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "充值手续费",
          //   prop: "fee",
          //   hide: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入充值手续费",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "充值金额",
            prop: "amount",
            rules: [
              {
                required: true,
                message: "请输入充值金额",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "结算时间",
          //   prop: "checkDate",
          //   hide: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入结算时间",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "充值状态",
            prop: "status",
            type: "select",
            dicData: [
              {
                label: "成功",
                value: 1,
              },
              {
                label: "不成功",
                value: 0,
              },
            ],
            rules: [
              {
                required: true,
                message: "请输入结算状态：1、已结算；0、未结算",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "充值设备",
          //   prop: "device",
          //   hide: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入充值设备",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "充值人",
            prop: "memberName",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入充值人",
                trigger: "blur",
              },
            ],
          },
          {
            label: "结算户",
            prop: "accountName",
            searchslot: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入结算户",
                trigger: "blur",
              },
            ],
          },
          {
            label: "备注",
            prop: "remarks",
            rules: [
              {
                required: true,
                message: "请输入备注",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
      datadialog: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.recharge_add, false),
        viewBtn: this.vaildData(this.permission.recharge_view, false),
        delBtn: this.vaildData(this.permission.recharge_delete, false),
        editBtn: this.vaildData(this.permission.recharge_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    searchResetDialog() {
      this.customerName = "";
      this.pagedialog.pageSize = 10;
      this.pagedialog.currentPage = 1;
      this.onLoadCustor();
    },
    selectRow(row) {
      this.dialogVisible = false;
      this.query.accountName = row.supplyCompany;
    },
    searchChangeDialog(params, done) {
      this.customerName = params.customerName;
      this.pagedialog.pageSize = 10;
      this.pagedialog.currentPage = 1;
      this.onLoadCustor();
      done();
    },
    onLoadCustor() {
      const { pagedialog, customerName } = this;
      getCustorList(pagedialog.currentPage, pagedialog.pageSize, {
        customerName,
      }).then((res) => {
        const data = res.data.data;
        this.pagedialog.total = data.total;
        this.datadialog = data.records.map((item) => {
          return {
            ...item,
            regionId: item.regionId.toString(),
            customerType: item.customerType.toString(),
            customerClass: item.customerClass.toString(),
            dangerLevel: item.dangerLevel.toString(),
            industryClass: item.industryClass.toString(),
            creditLevel: item.creditLevel.toString(),
            powerType: item.powerType.toString(),
            transferSupplyType: item.transferSupplyType.toString(),
            contactClass: item.contactClass.toString(),
            gender: item.gender.toString(),
            identifyType: item.identifyType.toString(),
          };
        });
      });
    },
    getExcel() {
      // 导出表格
      this.$refs.crud.rowExcel();
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = {
        ...this.query,
        ...params,
      };
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let objs = {
        startDate: this.query.createTime ? this.query.createTime[0] : "",
        endDate: this.query.createTime ? this.query.createTime[1] : "",
        accountName: this.query.accountName,
        chargeType: this.query.chargeType,
        memberName: this.query.memberName,
      };
      getList(page.currentPage, page.pageSize, Object.assign(objs)).then(
        (res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        }
      );
    },
  },
};
</script>

<style>
</style>
